//
//  JWTopMovieCell.m
//  JWMovie
//
//  Created by Jorn.Wu on 15/7/20.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "JWTopMovieCell.h"
#import "JWTopMovieModel.h"
#import "Star.h"
#import "UIImageView+WebCache.h"

@interface JWTopMovieCell ()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *title;
@property (nonatomic, strong) UIView *star;
@property (nonatomic, strong) UILabel *rate;

@end

@implementation JWTopMovieCell

- (void)awakeFromNib {
    // Initialization code
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {

        self.frame = frame;
        //        电影图片
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:imageView];
        imageView.backgroundColor = [UIColor grayColor];
        _imageView = imageView;
        
        //        电影标题
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        titleLabel.font = [UIFont systemFontOfSize:15];
        titleLabel.backgroundColor = [UIColor blackColor];
        titleLabel.alpha = 0.7f;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = [UIColor whiteColor];
        [_imageView addSubview:titleLabel];
        _title = titleLabel;
        
        //        电影评分
        UILabel *rateLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        rateLabel.font = [UIFont systemFontOfSize:13];
        rateLabel.textAlignment = NSTextAlignmentCenter;
        rateLabel.textColor = [UIColor orangeColor];
//        rateLabel.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:rateLabel];
        _rate = rateLabel;
        
        //        电影星级
        
        UIView *starView = [[UIView alloc] initWithFrame:CGRectZero];
//        starView.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:starView];
        _star = starView;
        
    }
    return self;
}

- (void)setCellFrame:(CGRect)frame{
    self.frame = frame;
    //        电影图片
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    [self.contentView addSubview:imageView];
    _imageView = imageView;
    
    //        电影标题
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    titleLabel.font = [UIFont systemFontOfSize:13];
    titleLabel.backgroundColor = [UIColor blackColor];
    titleLabel.alpha = 0.3f;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    [_imageView addSubview:titleLabel];
    _title = titleLabel;
    
    //        电影评分
    UILabel *rateLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    rateLabel.font = [UIFont systemFontOfSize:11];
    rateLabel.textAlignment = NSTextAlignmentCenter;
    rateLabel.textColor = [UIColor orangeColor];
    [self.contentView addSubview:rateLabel];
    _rate = rateLabel;
    
    //        电影星级
    
    UIView *starView = [[UIView alloc] initWithFrame:CGRectZero];
    starView.backgroundColor = [UIColor grayColor];
    [self.contentView addSubview:starView];
    _star = starView;
}

- (void)setTopMovieModel:(JWTopMovieModel *)topMovieModel{
    
    _topMovieModel = topMovieModel;
    
    //        电影图片
    
    NSString *urlStr = [_topMovieModel.images objectForKey:@"medium"];
//    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]];
//    UIImage *movieImage = [UIImage imageWithData:imageData];
//    self.imageView.image = movieImage;
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:urlStr]];
    
    //        电影标题
    
    self.title.text = _topMovieModel.title;
    
    //        电影评分
    
    NSNumber *number = [_topMovieModel.rating objectForKey:@"average"];
    NSString *rateStr = [NSString stringWithFormat:@"%.1f",[number floatValue]];
    _rate.text = rateStr;
    
    //        电影星级
    
    Star *star = [[Star alloc] initWithFrame:CGRectMake(0, 0, 90, 20)];
    star.max_star = 10;
    star.show_star = [number floatValue];
    star.font_size = 15;
    //    star.empty_color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"gray@2x"]];
    //    star.full_color = [UIColor colorWithPatternImage:[UIImage imageNamed: @"yellow@2x"]];
    star.empty_color = [UIColor grayColor];
    star.full_color = [UIColor colorWithRed:255 / 255.f green:185 / 255.f blue:15 / 255.f alpha:1];
    
    [self.star addSubview:star];
    
   
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    self.imageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 20);
    
    self.title.frame = CGRectMake(0, self.imageView.frame.size.height - 20, self.imageView.frame.size.width, 20);
    
    self.star.frame = CGRectMake(0, self.frame.size.height - 20, self.frame.size.width - 30, 20);
    
    self.rate.frame = CGRectMake(self.frame.size.width - 30, self.star.frame.origin.y, 30, 20);
    
  
}

+ (instancetype)topMovieCellWithCollectionView:(UICollectionView *)collectionView reuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath{
    
    JWTopMovieCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    return cell;
}

@end








